package com.zdy.adapter;

import android.app.Activity;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.networklibrary.SharedPreferencesUtils;
import com.example.networklibrary.config.KeyInterface;
import com.example.networklibrary.model.ProductListModel;
import com.zdy.showgoods.ActivityProduct;
import com.zdy.showgoods.R;
import com.zdy.utils.AppUtil;
import com.zdy.utils.DensityUtils;
import com.zdy.utils.GlideApp;

import java.util.List;

/**
 * Created by zhuangdaoyuan on 2018/7/13.
 * 安静撸码，淡定做人
 */
public class ProductListAdapter extends BaseQuickAdapter<ProductListModel, BaseViewHolder> {

    private ActivityProduct appCompatActivity;
    private String addPre;
    private Activity activity;

    public ProductListAdapter(Activity activity, @Nullable List<ProductListModel> data, ActivityProduct activityProduct) {
        super(R.layout.item_product, data);
        this.activity = activity;
        this.appCompatActivity = activityProduct;
        addPre = AppUtil.returnImgURL(appCompatActivity);
    }

    @Override
    protected void convert(BaseViewHolder helper, ProductListModel item) {
        ImageView img = helper.itemView.findViewById(R.id.item_product_item_img);
        ViewGroup.LayoutParams layoutParams = img.getLayoutParams();
        int imgWidth = (int) ((DensityUtils.getWidth(activity)-DensityUtils.dp2px(activity,80) )/ 3);
        layoutParams.width = imgWidth;
        img.setLayoutParams(layoutParams);
        img.setMaxWidth(imgWidth);
        if (TextUtils.isEmpty(item.getImage_id())) {
            GlideApp.with(appCompatActivity).load(R.mipmap.img_test_3).into(img);
        } else {
            String imgURL = addPre + item.getImage_id();
            GlideApp.with(appCompatActivity).load(imgURL).diskCacheStrategy(DiskCacheStrategy.RESOURCE).error(R.mipmap.img_test_3).into(img);
        }
        helper.setOnClickListener(R.id.item_product_item_img, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                appCompatActivity.toInfoActivity(item);
            }
        });
    }
}
